#[derive(Debug)]
struct People {
    name: String,
    color: String,
    age: u8,
}

// struct People<'a> {
//     // 定义字符串字面量，就需要自己标注生命周期
//     name: &'a str,
//     color: String,
//     age: u8,
// }

// 参数定义字符串字面量，可以兼容【字符串字面量】和【字符串引用】
fn print_string_1(s: &str) {
    println!("string: {s}")
}

// 参数定义字符串引用，只能兼容【字符串引用】
fn print_string_2(s: &String) {
    println!("string: {s}")
}

fn main() {
    // String
    // 创建方式
    let s = String::from("hello");
    // to_string, to_owned是字面量转字符串
    let s = "hello".to_string();
    let s = "hello".to_owned();
    println!("String: {s}");

    // &str
    // 可以直接编码ascii字符串
    let s = "\x23\x45\x67";
    println!("&str: {s}");

    // String使用场景(结构体)
    let people = People {
        name: String::from("Jackson"),
        color: String::from("Black"),
        age: 30,
    };

    println!("people: {:?}", people);

    // &str使用场景(函数)
    let s1 = "hello";
    let s2 = String::from("world");
    print_string_1(s1);
    print_string_1(&s2);

    // print_string_2(s1);
    print_string_2(&s2);

    println!("------------------------");

    // push方法，往字符串后面添加字符
    let mut s = String::from("hello ");
    s.push('X');
    println!("s: {}", s);

    // push_str方法，往字符串后面添加字符串
    let mut s = String::from("hello ");
    s.push_str("rust");
    println!("s: {}", s);

    // replace方法，给字符串做替换
    let s = String::from("hello world!");
    let s = s.replace("world", "rust");
    println!("s: {}", s);

    // contains方法，用来判断字符串是否包含某字符串
    let s = String::from("hello rust!");
    let b1 = s.contains("python");
    let b2 = s.contains("rust");
    println!("b1: {}; b2: {}", b1, b2);

    // format!宏，可以做字符串拼接，并且不会改变原有变量所有权
    let s1 = String::from("hello");
    let s2 = String::from("rust");
    let s3 = format!("{} {}", s1, s2);
    println!("{} + {} = {}", s1, s2, s3);

    // split_whitespace方法，将字符串中的空格切分开
    let s = String::from("hello rust!");
    let list = s.split_whitespace();
    for i in list {
        println!("{i}");
    }

    // chars方法，获取字符串中的字符集
    let s = String::from("12345");
    for i in s.chars() {
        println!("{i}");
    }

    // bytes方法，获取字符串每个字节
    let s = String::from("12345");
    for i in s.bytes() {
        println!("{i}");
    }
}
